package com.qf.redpack.business.web.listener;

import cn.hutool.json.JSONUtil;
import com.qf.redpack.business.web.service.RedpackGetService;
import com.qf.redpack.entity.RedpackGet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * 抢红包的消息监听器
 */
@Component
public class RobMsgListener {

    @Autowired
    private RedpackGetService redpackGetService;

    @KafkaListener(topics = "red-topic")
    public void robMsg(String msg){
        System.out.println("接收到抢红包的消息：" + msg);
        HashMap hashMap = JSONUtil.toBean(msg, HashMap.class);
        String rid = (String) hashMap.get("rid");
        String uid = (String) hashMap.get("uid");
        double robMoney = Double.valueOf(hashMap.get("robMoney").toString());

        redpackGetService.save(new RedpackGet()
                .setUid(Integer.parseInt(uid))
                .setRid(Long.parseLong(rid))
                .setGetMoney(robMoney));
    }
}
